package com.banciyuan.bcywebview.base.util;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 观察者模式中的被观察者
 * 主要用观察者模式模拟事件响应和数据同步
 * 类似简易的eventbus
 */
public class BaseObserver {

    public static final int COMMENT_EVENT = 100;
    public static final int LIKE_EVENT = 101;
    public static final int RECOMMEND_EVENT = 102;
    public static final int FOLLOW_EVENT = 103;
    public static final int FOLLOW_EVENT_REFREASH = 120;
    public static final int MESSAGE_EVENT = 105;
    public static final int MAINSETTING_EVENT = 106;
    public static final int MAINSETTING_NOTICE_EVENT = 107;
    public static final int MAINSETTING_UNNOTICE_EVENT = 108;
    public static final int GO_PERSON = 129;

    public static final int DELELE_COMMENT_EVENT = 126;
    public static final int LIKE_COMMENT_EVENT = 127;
    public static final int DISLIKE_COMMENT_EVENT = 128;
    public static final int DELELE_REPLY_EVENT = 180;

    public static final int DELETE_EVENT = 121;
    public static final int FINISH_EVENT = 122;
    public static final int CHARGE_EVENT = 123;
    public static final int SHARE_EVENT = 124;
    public static final int CHARGE_RANK = 125;

    public static final int USER_DATA_CHANGE_EVENT = 111;
    public static final int USER_NEED_RECOMMEND_EVENT = 112;

    public static final int GROUP_COUNT_CLEAR = 113;

    public static final int DELETE = 114;
    public static final int CITY_CILICK = 140;
    public static final int COPY = 130;

    public static final int TEAM_FOCUSED_CLEAR = 115;
    public static final int CHANNAL_GODISCUSS=131;
    public static final int CIRCLE_GODISCUSS=151;
    public static final int CIRCLE_GOGOODS=152;
    public static final int CIRCLE_GONEWEST=153;
    public static final int CIRCLE_GOTOPUSER=154;
    public static final int CIRCLE_GOTOPUSER_INNER=158;


    public static final int COMPELATE=150;
    public static final int START_LOADING=151;
    public static final int SHOW_BOTTOM=152;
    public static final int HIDE_BOTTOM=153;

    public static final int REFRESH_PERSON = 155;
    public static final int GOTO_TABCIRCLE = 157;


    public static final int NOVEL_GO_INDEX=180;
    public static final int NOVEL_DO_BOOK=181;
    public static final int NOVEL_DONOT_BOOK=182;
    public static final int NOVEL_GO_NEXT=183;
    public static final int NOVEL_GO_PRE=184;

    public static final int LIGHT=185;
    public static final int TAKE_PHOTO=190;
    public static final int PHOTO_PERVIEW=191;
    public static final int PHOTO_SELECT=192;
    public static final int CHANGE_NUM=193;

    public static final int ASK_REFRESH=201;
    public static final int REPORT_SUCCESS = 202;
    public static final int RANK_REFREASH=203;
    public static final int VIDEO_PLAY=300;
    public static final int SEARCH_GOTO_USER=400;

    public static final int FOCUS_USER_HAS_POST=501;


    private static BaseObserver myOb;

    public static BaseObserver getInstance() {
        if (myOb == null) {
            synchronized (BaseObserver.class) {
                if (myOb == null) myOb = new BaseObserver();
            }
        }
        return myOb;
    }

    // 存放观察者
    private Map<String, BaseWatcher> list = new ConcurrentHashMap<>();

    public void addWatcher(Class name, BaseWatcher watcher) {
        list.put(name.getName(), watcher);
    }

    public void removeWatcher(Class name) {
        list.remove(name.getName());
    }

    public void notifyWatchers(int eventCode) {
        notifyWatchers(eventCode, null);
    }

    public void notifyWatchers(int eventCode, String message) {
        notifyWatchers(eventCode, message, null);
    }

    public void notifyWatchers(int eventCode, Object object) {
        notifyWatchers(eventCode, null, object);
    }

    public void notifyWatchers(int eventCode, String message, Object object) {
        for (BaseWatcher watcher : list.values()) {
            watcher.onEventHappen(eventCode, message, object);
        }
    }
}
